package com.chahu.servlet.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chahu.common.Result;
import com.chahu.entity.Order;
import com.chahu.service.PurchaseService;
import com.chahu.service.impl.PurchaseServiceImpl;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/purchase")
public class PurchaseServlet extends HttpServlet {
    PurchaseService purchaseService;

    @Override
    public void init() throws ServletException {
        purchaseService = new PurchaseServiceImpl();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json; charset=utf-8");

        String json = IOUtils.toString(req.getReader());

        JSONObject data = JSONObject.parseObject(json);

        String name = data.getString("name");
        String address = data.getString("address");
        String mail = data.getString("mail");
        String phone = data.getString("phone");
        String note = data.getString("note");
        Integer payment = data.getInteger("payment");
        Integer delivery = data.getInteger("delivery"); 
        Order order = new Order();
        order.setName(name);
        order.setAddress(address);
        order.setMail(mail);
        order.setPhone(phone);
        order.setNote(note);
        order.setPayment(payment);
        order.setDelivery(delivery);
        purchaseService.order(order);
    }
}
